<?php

namespace app\admin\model;

use think\Model;


class Machine extends Model
{
    // 表名
    protected $name = 'machine';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
//    protected $append = [
//        'state_text'
//    ];
    
    public function getStateList()
    {
        return ['0' => __('State 0'), '1' => __('State 1')];
    }


    public function getStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['state']) ? $data['state'] : '');
        $list = $this->getStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function todayProfit()
    {
        $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
        $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
        $map['createtime'] = array('between', array($beginToday,$endToday));
        return $this->hasOne('MachineProfitLog','machine_id','id')->where($map)->bind('profit');
    }
    public function goodsInfo()
    {
        return $this->hasOne('Goods','id','goods_id')->bind('title');
    }

}
